package jmind.core.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class ZipMyClient3 {

    private final static Logger logger = Logger.getLogger(ZipMyClient3.class.getName());

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10; i++) {
            final int idx = i;
            new Thread(new MyRunnable(idx)).start();
        }
    }

    private static final class MyRunnable implements Runnable {

        private final int idx;

        private MyRunnable(int idx) {
            this.idx = idx;
        }

        public void run() {
            SocketChannel socketChannel = null;
            try {
                socketChannel = SocketChannel.open();
                SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);
                socketChannel.connect(socketAddress);

                MyRequestObject myRequestObject = new MyRequestObject("request_" + idx, "request_" + idx);
                //  logger.log(Level.INFO, myRequestObject.toString());
                sendData(socketChannel, myRequestObject);

                MyRequestObject myResponseObject = receiveData(socketChannel);
                System.out.println("client receive=" + myResponseObject);

            } catch (Exception ex) {
                logger.log(Level.SEVERE, null, ex);
            } finally {
                try {
                    socketChannel.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

    public static void sendData(SocketChannel socketChannel, MyRequestObject myRequestObject) throws IOException {
        Socket socket = socketChannel.socket();
        GZIPOutputStream out = new GZIPOutputStream(socket.getOutputStream());
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(myRequestObject);
        oos.flush();
        out.finish();

    }

    public static MyRequestObject receiveData(SocketChannel socketChannel) throws IOException {
        MyRequestObject myRequestObject = null;
        ObjectInputStream ois = null;
        Socket socket = socketChannel.socket();

        try {
            GZIPInputStream gzipis = new GZIPInputStream(socket.getInputStream());
            ois = new ObjectInputStream(gzipis);

            Object obj = ois.readObject();
            myRequestObject = (MyRequestObject) obj;
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //  ois.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return myRequestObject;
    }
}
